﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using PolePositionMobileUtilityEntity.Entities;
using System.Data;
using PolePositionMobileUtilityData;
using PolePositionMobileUtilityGeneral;
using System.Data.SqlServerCe;

namespace PolePositionMobileUtilityEntity.Logic
{
    public class LSetting
    {
        public static ESetting GetAll()
        {
            ESetting _Setttings = new ESetting();
            DataTable _Dt = DSetting.GetAll();
            foreach (DataRow _Dr in _Dt.Rows)
            {
                switch (Int32.Parse(_Dr["SettingKey"].ToString()))
                {

                    case (Int32)SettingsType.Company:
                        _Setttings.Company = new ECompany(ConvertMobileHelper.ToInt32(_Dr["SettingValue"]), ConvertMobileHelper.Tostring(_Dr["SettingName"]));
                        break;
                    case (Int32)SettingsType.Inspector:
                        _Setttings.Inspector = new EInspector(ConvertMobileHelper.ToInt32(_Dr["SettingValue"]), ConvertMobileHelper.Tostring(_Dr["SettingName"]));
                        break;
                    case (Int32)SettingsType.Vendor:
                        _Setttings.Vendor = new EVendor(ConvertMobileHelper.ToInt32(_Dr["SettingValue"]), ConvertMobileHelper.Tostring(_Dr["SettingName"]));
                        break;
                    case (Int32)SettingsType.Project:
                        _Setttings.Project = new EProject(ConvertMobileHelper.ToInt32(_Dr["SettingValue"]), ConvertMobileHelper.Tostring(_Dr["SettingName"]));
                        break;
                    case (Int32)SettingsType.User:
                        _Setttings.Contact= new EContact(ConvertMobileHelper.ToInt32(_Dr["SettingValue"]), ConvertMobileHelper.Tostring(_Dr["SettingName"]));
                        break;
                    case (Int32)SettingsType.ReceptionMode:
                        _Setttings.ReceptionMode = ConvertMobileHelper.ToInt32(_Dr["SettingValue"]);
                        break;
                }
            }
            return _Setttings;
        }


        public static void Insert(ESetting setting)
        {
            SqlCeTransaction _Tr = null;
            SqlCeConnection _Con = null;
            try
            {
                _Con = Utils.GetConnection();
                _Tr = _Con.BeginTransaction();
                InsertType(SettingsType.Company, setting.Company.Id, setting.Company.Name, _Tr);
                InsertType(SettingsType.Inspector, setting.Inspector.Id, setting.Inspector.Name, _Tr);
                InsertType(SettingsType.Vendor, setting.Vendor.Id, setting.Vendor.Name, _Tr);
                InsertType(SettingsType.User, setting.Contact.Id, setting.Contact.Name, _Tr);
                InsertType(SettingsType.Project, setting.Project.Id, setting.Project.Name, _Tr);
                InsertType(SettingsType.ReceptionMode, setting.ReceptionMode, "ReceptionMode", _Tr);

                _Tr.Commit();
            }
            catch (Exception _Ex)
            {
                if (_Tr != null)
                    _Tr.Rollback();
                throw _Ex;
            }
            finally
            {
                if (_Con != null)
                {
                    _Con.Close();
                }
            }
            
        }

        private static void InsertType(SettingsType type, int value, string name, SqlCeTransaction tr)
        {
            DSetting.DeleteByType((Int32)type, tr);
            DSetting.Insert((Int32)type, name, value, tr);
        }
    }
}
